package com.app.TCES.interceptor;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.app.TCES.common.MemoryData;

@Component
public class SingleUserInterceptor implements HandlerInterceptor {
	
	Logger log =LogManager.getLogger(SingleUserInterceptor.class);
	
	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		String uri = request.getRequestURI();

		String role = (String) request.getSession().getAttribute("userRole");
		String userName = (String) request.getSession().getAttribute("userName");
		
		if ( uri.indexOf("/Login") >= 0 || uri.indexOf("/Checklogin") >= 0) {
			
			return true;
			
		} else if (uri.indexOf("/Logout") >= 0) {
			MemoryData.getSessionIDMap().remove(userName);
			String indexurl=request.getContextPath()+"/Login";
			response.sendRedirect(indexurl);
			return false;
		}else {
			if (userName == null) {
				request.getRequestDispatcher(request.getContextPath() + "/Login").forward(request, response);
				return false;
			} else {
				if (uri.indexOf("/Student")>=0||uri.indexOf("/MyCourse")>=0||uri.indexOf("/StudentPJ")>=0||uri.indexOf("/StudentCP")>=0) {
					if (role.equals("student")) {
						
						return true;
//						String sessionId =MemoryData.getSessionIDMap().get(userName);
//						if (sessionId.equals(request.getSession().getId())) {
//							return true;
//						}else { 
//							//如果请求的sessionID和此账号Map中存放的sessionID不一致，跳转到登陆页 
//							//判断如果是异步请求，设置响应头 sessionstatus为timeout，自动跳转，否则重定向
//							if(request.getHeader("x-requested-with")!=null&& request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){ 
//								response.setHeader("sessionstatus","timeout");
//								
//								return false;
//							}else{
//								String indexurl=request.getContextPath()+"/Login";
//								response.sendRedirect(indexurl);
//								return false;
//							}
//						}
					}else {
						String indexurl=request.getContextPath()+"/Login";
						response.sendRedirect(indexurl);
						return false;
					}
				}else if(uri.indexOf("/Teacher")>=0||uri.indexOf("/TeacherCourse")>=0||uri.indexOf("/TeacherScore")>=0||uri.indexOf("/TeacherPJ")>=0||uri.indexOf("/TeacherCP")>=0){
					if (role.equals("teacher")) {
						
						return true;
//						String sessionId =MemoryData.getSessionIDMap().get(userName);
//						if (sessionId.equals(request.getSession().getId())) {
//							return true;
//						}else {
//							
//							//如果请求的sessionID和此账号Map中存放的sessionID不一致，跳转到登陆页 
//							//判断如果是异步请求，设置响应头 sessionstatus为timeout，自动跳转，否则重定向
//							if(request.getHeader("x-requested-with")!=null&& request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){ 
//								response.setHeader("sessionstatus","timeout");
//								log.info("账号已登录");
//								return false;
//							}else{
//								String indexurl=request.getContextPath()+"/Login";
//								response.sendRedirect(indexurl);
//								return false;
//							}
//						}
					}else {
						String indexurl=request.getContextPath()+"/Login";
						response.sendRedirect(indexurl);
						return false;
					}
				}else if(uri.indexOf("/Admin_index")>=0||uri.indexOf("/Admin_teacher")>=0||uri.indexOf("/Admin_student")>=0||uri.indexOf("/Admin_dept")>=0||uri.indexOf("/Admin_class")>=0||uri.indexOf("/Admin_course")>=0||uri.indexOf("/Admin_course_class")>=0||uri.indexOf("/Admin_evaluate1")>=0||uri.indexOf("/Admin_evaluate2")>=0||uri.indexOf("/Admin_evaluate3")>=0){
					if (role.equals("admin")) {
						
						return true;
//						String sessionId =MemoryData.getSessionIDMap().get(userName);
//						
//						if (sessionId.equals(request.getSession().getId())) {
//							return true;
//						}else {
//							//如果请求的sessionID和此账号Map中存放的sessionID不一致，跳转到登陆页 
//							//判断如果是异步请求，设置响应头 sessionstatus为timeout，自动跳转，否则重定向
//							if(request.getHeader("x-requested-with")!=null&& request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){ 
//								response.setHeader("sessionstatus","timeout");
//								log.info("账号已登录");
//								return false;
//							}else{
//								String indexurl=request.getContextPath()+"/Login";
//								response.sendRedirect(indexurl);
//								return false;
//							}
//						}
						
					}else {
						String indexurl=request.getContextPath()+"/Login";
						response.sendRedirect(indexurl);
						return false;
					}
				}else {
					return true;
				}
				
			}
		}
		
	}

}
